package com.zuipin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zuipin.entity.product.ProductCollection;
import com.zuipin.framework.annotation.Inject;
import com.zuipin.framework.annotation.Json;
import com.zuipin.framework.annotation.Jsons;
import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.result.PageResult;
import com.zuipin.framework.result.Result;
import com.zuipin.service.IProductCollectionService;
import com.zuipin.service.IProductInfoService;
import com.zuipin.util.Pagination;
import com.zuipin.util.StringUtil;
import com.zuipin.vo.IntegralProVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 
 *
 * @ClassName ProductCollectionController
 * @author wangcongyi
 * @date 2017年6月30日 下午4:32:55
 */
@Api(value = "商品集合", description = "商品集合-wangcongyi")
@Controller
@RequestMapping(value = "/productCollection")
public class ProductCollectionController {
	
	@Resource
	private IProductCollectionService	productCollectionService;
	@Resource
	private IProductInfoService			productInfoService;
	
	@ResponseBody
	@RequestMapping(value = "/findProductCollection", method = RequestMethod.POST)
	@ApiOperation(value = "三期-分页查询商品集合信息(后台)-wangcongyi", notes = "分页查询商品集合信息" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数错误" + "</br>1002-必填项未填写" + "</br>1004-系统异常")
	@Valids({ @Valid(name = "collection.collectionName"), @Valid(name = "collection.proSkus"), @Valid(name = "collection.sysId", required = true),
			@Valid(name = "collection.state"), @Valid(name = "page.pageNumber", required = true), @Valid(name = "page.pageSize", required = true), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "collection.collectionName", value = "商品集合名称", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "collection.proSkus", value = "商品SKU", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "collection.sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "collection.state", value = "集合状态 1 正常| 0禁用", dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "page.pageNumber", value = "页码", required = true, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "page.pageSize", value = "单页记录数", required = true, dataType = "integer", paramType = "query"), })
	public Result<List<ProductCollection>> findProductCollection(@Inject @ApiIgnore ProductCollection collection, @Inject @ApiIgnore Pagination page) {
		PageResult<List<ProductCollection>> result = new PageResult<>();
		result.setData(productCollectionService.findProductCollection(collection, page));
		result.setTotal_record(page.getTotalRecord());
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/getCurrentNo", method = RequestMethod.GET)
	@ApiOperation(value = "三期-获取当前商品集合编号-wangcongyi", notes = "获取当前商品集合编号" + "</br>返回码" + "</br>0-成功" + "</br>1004-系统异常")
	public Result<Map<String, Object>> getCurrentNo() {
		Result<Map<String, Object>> result = new Result<>();
		Map<String, Object> map = new HashMap<>();
		String maxNo = productCollectionService.getMaxNo();
		if (null != maxNo) {
			Integer currentNo = Integer.parseInt(maxNo);
			map.put("NO", "JH" + StringUtil.fillStr(String.valueOf(++currentNo), 5, false, "0"));
		} else {
			map.put("NO", "JH00001");
		}
		result.setData(map);
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/modifyState", method = RequestMethod.GET)
	@ApiOperation(value = "三期-(禁用/启用商品集合)-wangcongyi", notes = "(禁用/启用商品集合)" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数错误" + "</br>1002-必填项未填写" + "</br>1003-商品修改状态失败"
			+ "</br>1004-系统异常" + "</br>2000-商品集合不存在或被删除")
	@Valids({ @Valid(name = "collection.id", required = true), @Valid(name = "collection.state", required = true), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "collection.id", value = "商品集合ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "collection.state", value = "集合状态", required = true, dataType = "long", paramType = "query"), })
	public Result<Map<String, Object>> modifyState(@Inject @ApiIgnore ProductCollection collection) {
		Result<Map<String, Object>> result = new Result<>();
		StringBuilder stateStr = new StringBuilder();
		// 判断用户传值
		if (collection.getState().equals(ProductCollection.NORMAL)) {
			stateStr.append("启用");
		} else if (collection.getState().equals(ProductCollection.FORBIDDEN)) {
			stateStr.append("禁用");
		} else {
			result.setErrorCode(ErrorCode.STATE_INVALID);
			result.setMessage("输入无效状态值！");
			return result;
		}
		ProductCollection productCollection = productCollectionService.selectByPrimaryKey(collection.getId());
		if (null != productCollection) {
			// 更新状态
			boolean falg = productCollectionService.modifyState(collection);
			if (falg) {
				result.setErrorCode(ErrorCode.SUCCESS);
				result.setMessage("商品[" + productCollection.getCollectionNo() + "]" + stateStr + "成功！");
			} else {
				result.setErrorCode(ErrorCode.DB_EXCEPTION);
				result.setMessage("商品[" + productCollection.getCollectionNo() + "]" + stateStr + "失败！");
			}
		} else {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setMessage("该商品集合不存在或者被删除");
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/deleteCollection", method = RequestMethod.GET)
	@ApiOperation(value = "删除商品集合-wangcongyi", notes = "删除商品集合" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数错误" + "</br>1002-必填项未填写" + "</br>1004-系统异常")
	@Valids({ @Valid(name = "collection.id", required = true), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "collection.id", value = "商品集合ID", required = true, dataType = "long", paramType = "query"), })
	public Result<Map<String, Object>> deleteCollection(@Inject @ApiIgnore ProductCollection collection) {
		Result<Map<String, Object>> result = new Result<>();
		// 业务逻辑
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setMessage("删除成功！");
		return result;
	}
	
	@Jsons({ @Json(type = ProductCollection.class, exclude = "updateId,updateTime,createId,delFlag") })
	@RequestMapping(value = "/getCollectionDetail", method = RequestMethod.GET)
	@ApiOperation(value = "三期-获取商品集合信息详情-wangcongyi", notes = "获取商品集合信息详情" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数错误" + "</br>1002-必填项未填写" + "</br>1004-系统异常"
			+ "</br>2000-商品集合信息不存在")
	@Valids({ @Valid(name = "collection.id", required = true) })
	@ApiImplicitParams({ @ApiImplicitParam(name = "collection.id", value = "商品集合ID", required = true, dataType = "long", paramType = "query"), })
	public Result<ProductCollection> delete(@Inject @ApiIgnore ProductCollection collection) {
		Result<ProductCollection> result = new Result<>();
		JSONObject params = new JSONObject();
		ProductCollection productCollection = productCollectionService.selectByPrimaryKey(collection.getId());
		if (null != productCollection) {
			String proSkus = productCollection.getProSkus();
			String[] arr = proSkus.split(",");
			StringBuilder proSkuBuilder = new StringBuilder();
			String sp = "";
			for (int i = 0; i < arr.length; i++) {
				proSkuBuilder.append(sp);
				proSkuBuilder.append("'");
				proSkuBuilder.append(arr[i]);
				proSkuBuilder.append("'");
				sp = ",";
			}
			params.put("sysId", productCollection.getSysId());
			params.put("proSkus", proSkuBuilder.toString());
			Pagination page = new Pagination();
			page.setPageNumber(1);
			page.setPageSize(arr.length);
			productCollection.setProducts(productInfoService.findPro(params, page));
			result.setData(productCollection);
			result.setErrorCode(ErrorCode.SUCCESS);
		} else {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setMessage("商品集合信息不存在！");
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/createCollection", method = RequestMethod.POST)
	@ApiOperation(value = "三期-添加商品集合信息-wangcongyi", notes = "添加商品集合信息" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数错误" + "</br>1002-必填项未填写" + "</br>1004-系统异常" + "<br/>3005-商品集合编号重复")
	@Valids({ @Valid(name = "collection.collectionName", required = true), @Valid(name = "collection.proSkus", required = true),
			@Valid(name = "collection.collectionNo", required = true), @Valid(name = "collection.sysId", required = true), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "collection.collectionName", value = "商品集合名称", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "collection.proSkus", value = "商品集合SKU集", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "collection.collectionNo", value = "商品集合编号", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "collection.sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"), })
	public Result<Map<String, Object>> createCollection(@Inject @ApiIgnore ProductCollection collection) {
		Result<Map<String, Object>> result = new Result<>();
		ProductCollection productCollection = productCollectionService.isExists(collection);
		if (null != productCollection) {
			result.setErrorCode(ErrorCode.OBJECT_EXIST);
			result.setMessage("商品集合编号已经存在！");
		} else {
			productCollectionService.insertSelective(collection);
			result.setErrorCode(ErrorCode.SUCCESS);
			result.setMessage("添加成功！");
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/updateCollection", method = RequestMethod.POST)
	@ApiOperation(value = "三期-更新商品集合信息-wangcongyi", notes = "更新商品集合信息" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数错误" + "</br>1002-必填项未填写" + "</br>1004-系统异常" + "</br>2000-商品集合不存在"
			+ "<br/>3005-商品集合编号重复")
	@Valids({ @Valid(name = "collection.id", required = true), @Valid(name = "collection.collectionName"), @Valid(name = "collection.collectionNo"),
			@Valid(name = "collection.proSkus"), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "collection.id", value = "商品集合ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "collection.collectionName", value = "商品集合名称", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "collection.collectionNo", value = "商品集合编号", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "collection.proSkus", value = "商品集合SKU集", dataType = "string", paramType = "query"), })
	public Result<Map<String, Object>> updateCollection(@Inject @ApiIgnore ProductCollection collection) {
		Result<Map<String, Object>> result = new Result<>();
		ProductCollection coll = productCollectionService.selectByPrimaryKey(collection.getId());
		if (null != coll) {
			ProductCollection productCollection = productCollectionService.isExists(collection);
			if (null != productCollection) {
				if (!collection.getId().equals(productCollection.getId())) {
					result.setErrorCode(ErrorCode.OBJECT_EXIST);
					result.setMessage("商品集合编号已经存在！");
					return result;
				}
			}
			productCollectionService.updateByPrimaryKeySelective(collection);
			result.setErrorCode(ErrorCode.SUCCESS);
			result.setMessage("更新成功！");
		} else {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setMessage("商品集合不存在！");
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/getHotExchangeProCollection", method = RequestMethod.POST)
	@ApiOperation(value = "热门兑换 商品集合列表(前台)", notes = "热门兑换 商品集合列表(前台)")
	@Valids({ @Valid(name = "sysId", minValue = 1, required = true), @Valid(name = "device", regex = "PC|WAP", required = true), @Valid(name = "page.pageNumber", required = true),
			@Valid(name = "page.pageSize", required = true) })
	@ApiImplicitParams({ @ApiImplicitParam(name = "page.pageNumber", value = "页码", required = true, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "page.pageSize", value = "单页记录数", required = true, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "device", value = "设备", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "isDisplay", value = "是否显示已下架商品  0不显示1显示", required = false, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "collectionNo", value = "集合的编号", required = true, dataType = "string", paramType = "query") })
	public Result<List<IntegralProVo>> getHotExchangeProCollection(@RequestParam(required = false) Integer isDisplay, @RequestParam String collectionNo,
			@RequestParam String device, @RequestParam Long sysId, @Inject @ApiIgnore Pagination page) {
		if (isDisplay == null) {
			isDisplay = ProductCollection.NOT_DISPLAY;
		}
		return productCollectionService.getHotExchangeProCollection(isDisplay, collectionNo, device, sysId, page);
	}
	
	@ResponseBody
	@RequestMapping(value = "/getProCollectionByBack", method = RequestMethod.POST)
	@ApiOperation(value = "商品集合列表(后台)编号&名称的模糊查询", notes = "商品集合列表(后台)编号&名称的模糊查询")
	@Valids({})
	@ApiImplicitParams({ @ApiImplicitParam(name = "page.pageNumber", value = "页码", required = true, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "page.pageSize", value = "单页记录数", required = true, dataType = "integer", paramType = "query"), //
			@ApiImplicitParam(name = "seachCondition", value = "查询条件（编号|名称）", required = false, dataType = "string", paramType = "query"), //
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"), })
	public Result<List<ProductCollection>> getProCollectionByBack(String seachCondition, Long sysId, @Inject @ApiIgnore Pagination page) {
		PageResult<List<ProductCollection>> result = new PageResult<List<ProductCollection>>();
		List<ProductCollection> data = productCollectionService.getProCollectionByBack(seachCondition, sysId, page);
		result.setData(data);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setMessage("查询成功");
		result.setTotal_record(page.getTotalRecord());
		return result;
	}
	
}
